package cn.edu.xmu.javaee.productdemomybatis.controller;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.util.JacksonUtil;
import cn.edu.xmu.javaee.productdemomybatis.service.vo.CustomerProductVo;
import cn.edu.xmu.javaee.productdemomybatis.dao.bo.Product;
import cn.edu.xmu.javaee.productdemomybatis.service.ProductService;
import cn.edu.xmu.javaee.productdemomybatis.util.CloneFactory;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static cn.edu.xmu.javaee.core.model.Constants.PLATFORM;
import static cn.edu.xmu.javaee.productdemomybatis.util.Common.changeHttpStatus;
import static cn.edu.xmu.javaee.productdemomybatis.util.Common.getI18nMessage;

/**
 * 商品控制器
 * @author Ming Qiu
 */
@RestController /*Restful的Controller对象*/
@RequestMapping(value = "/products", produces = "application/json;charset=UTF-8")
@Slf4j
@RequiredArgsConstructor
public class ProductController {

    private final  ProductService productService;
    private final MessageSource messageSource;

    @GetMapping("{id}")
    public ReturnObject getProductById(@PathVariable("id") Integer id, HttpServletResponse response) {
        log.debug("getProductById: id = {} " ,id);
        MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(this.messageSource, LocaleContextHolder.getLocale());
        ReturnObject retObj = null;
        CustomerProductVo product;
        try {
            product = this.productService.retrieveProductByID(PLATFORM, id);
            retObj = new ReturnObject(ReturnNo.OK, messageSourceAccessor.getMessage(ReturnNo.OK.getMessage()), product);
        }
        catch (BusinessException e){
            retObj = new ReturnObject(e.getErrno(), getI18nMessage(e, messageSourceAccessor));
        }
        changeHttpStatus(retObj.getCode(), response);
        return  retObj;
    }



    @GetMapping("")
    public ReturnObject searchProductByName(@RequestParam String name, HttpServletResponse response) {
        MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(this.messageSource, LocaleContextHolder.getLocale());
        ReturnObject retObj = null;
        try{
            List<CustomerProductVo> data = this.productService.retrieveCustomerProductVoByName(PLATFORM, name);
            retObj = new ReturnObject(ReturnNo.OK, messageSourceAccessor.getMessage(ReturnNo.OK.getMessage()), data);
        }
        catch (BusinessException e){
            retObj = new ReturnObject(e.getErrno(), getI18nMessage(e,messageSourceAccessor));
        }
        changeHttpStatus(retObj.getCode(), response);
        return  retObj;
    }


}
